package com.ethan.threadPool;


import com.ethan.threadPool.Servcie.*;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListeningExecutorService;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @author ethan
 * create 2018-07-23 下午8:57
 * desc
 **/
public class Test {
    public static void main(String[] args) throws InterruptedException {
        Stopwatch stopwatch = Stopwatch.createStarted();
        List<Callable<BaseDataResponse>> taskList = Lists.newArrayListWithCapacity(BaseDataResponse.BaseDataType.values().length);

        for (BaseDataResponse.BaseDataType baseDataType: BaseDataResponse.BaseDataType.values()) {
            taskList.add(new BaseDataFetchTask(baseDataType,"xep123"));
        }

        ConcurrentProcessor concurrentProcessor = new ConcurrentProcessor("test");
        ListeningExecutorService listeningExecutorService = ThreadPoolManager.baseDataExecutorService;

        List<BaseDataResponse> baseDatasListResponse =  concurrentProcessor.fetchAllData(taskList,listeningExecutorService);


        System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS));

        String a ="";


    }

}

class BaseDataFetchTask implements Callable<BaseDataResponse>{
    private BaseDataResponse.BaseDataType baseDataType;
    private String orderNo;

    public BaseDataFetchTask(BaseDataResponse.BaseDataType baseDataType , String orderNo){
        this.baseDataType = baseDataType;
        this.orderNo = orderNo;
    }


    @Override
    public BaseDataResponse call() throws Exception {
        BaseDataResponse baseDataResponse = null;
        IService iService = new Service();
        switch (baseDataType){
            case FLIGHT:
                FlightSegment flightSegment = iService.getFlightSegment(this.orderNo);
                baseDataResponse  = BaseDataResponse.buildBaseDataResponse(baseDataType,flightSegment);
                break;
            case INSURANCE:
                Insurance insurance = iService.getInsurance(orderNo);
                baseDataResponse = BaseDataResponse.buildBaseDataResponse(baseDataType,insurance);
                break;
            default:
                break;
        }
        return baseDataResponse;
    }
}






//
//class Thread1 implements Callable<FlightSegment>{
//    private String orderNo;
//    private Double price;
//
//    public Thread1(String orderNo,Double price){
//        this.orderNo = orderNo;
//        this.price = price;
//    }
//
//    @Override
//    public FlightSegment call() throws Exception {
//        IService iService = new Service();
//        return iService.getFlightSegment(this.orderNo,this.price);
//    }
//
//
//}
//
//class Thread2 implements Callable<Insurance>{
//    private String orderNo;
//    private Double price;
//
//    public Thread2(String orderNo,Double price){
//        this.orderNo = orderNo;
//        this.price = price;
//    }
//
//    @Override
//    public Insurance call() throws Exception {
//        IService iService = new Service();
//        return iService.getInsurance(this.orderNo,this.price);
//    }
//
//}